﻿namespace MelodysDemo.Application.ClazzQuestionnaires
{
    public class ClazzQuestionnaireService : IClazzQuestionnaireService
    {
        readonly IRepository<SurveyClazzQuestionnaire, int> _clazzQuestionnaire;//班级问卷信息表
        public ClazzQuestionnaireService(IRepository<SurveyClazzQuestionnaire, int> clazzQuestionnaire)
        {
            _clazzQuestionnaire = clazzQuestionnaire;
        }
        public async Task<PagedResultDto<CQnaireDto>> GetResultDto(ClazzQuestionnaireInput input)
        {
            var q = _clazzQuestionnaire.GetAll()
                .Include(cq=>cq.Clazz)
                .Include(cq=>cq.Questionnaire)
                .Include(cq => cq.Clazz.SurveyClazzQuestionnaires)
                                                .Where(cq => cq.IsDelete == 0)
                                                .Select(cq => new CQnaireDto
                                                {
                                                    Id = cq.Id,//Id
                                                    ClazzName = cq.Clazz.ClazzName,//班级名
                                                    StudentCount = cq.Clazz.StudentCount,//班级人数
                                                    Title = cq.Questionnaire.Title,//问卷名
                                                    QuestionnaireId = cq.QuestionnaireId.ToString(),//问卷Id
                                                    Status = cq.Status,//状态
                                                    QuestionnaireFNumber = (short)cq.SurveyAnswers.Count,//填写人数
                                                    Fillingrate = cq.Clazz.Creator,//填写率
                                                    CreateTime = cq.CreateTime,//创建时间
                                                    ClazzId = cq.ClazzId,//班级Id
                                                    IsIpLimit = cq.IsIpLimit//IP限制
                                                });

            //班级名
            if (input.ClazzName is { Length: > 0 })
            {
                q = q.Where(cq => cq.ClazzName.Contains(input.ClazzName));
            }
            //问卷名
            if (input.Title is { Length: > 0 })
            {
                q = q.Where(cq => cq.Title.Contains(input.Title));
            }
            //问卷ID
            if (input.QuestionnaireId is { Length: > 0 })
            {
                q = q.Where(cq => cq.QuestionnaireId == input.QuestionnaireId);
            }
            //状态
            if (input.Status < 3)
            {
                q = q.Where(cq => cq.Status == input.Status);
            }
            //开始时间
            if (input.Begin is { Length: > 0 })
            {
                q = q.Where(cq => cq.CreateTime >= DateTime.Parse(input.Begin));
            }
            //结束时间
            if (input.End is { Length: > 0 })
            {
                q = q.Where(cq => cq.CreateTime <= DateTime.Parse(input.End));
            }

            var count = q.Count();
            var list = await q.OrderByDescending(cq => cq.Id)
                                               .Skip((input.Page - 1) * input.Limit).Take(input.Limit)
                                               .AsNoTracking().ToListAsync();
            return new PagedResultDto<CQnaireDto>
            {
                Page = input.Page,
                Limit = input.Limit,
                TotalCount = count,
                Data = list
            };
        }
    }
}
